package com.xinchuang.utils;

import com.alibaba.fastjson.JSONArray;

/**
 * TODO
 *
 * @author zkwczx
 * @version 1.0
 * @date 2024/6/3 20:23
 */
public class CenterUtil {

    static class Point {
        double x, y;

        Point(double x, double y) {
            this.x = x;
            this.y = y;
        }
    }

    public static double getBearing(Point p1, Point p2) {
        double dx = p2.x - p1.x;
        double dy = p2.y - p1.y;
        return Math.toDegrees(Math.atan2(dy, dx));
    }

    public static void translateRectangle(Point[] diagonalPoints, Point oldCenter, Point newCenter) {
        double dx = newCenter.x - oldCenter.x;
        double dy = newCenter.y - oldCenter.y;

        for (Point p : diagonalPoints) {
            p.x += dx;
            p.y += dy;
        }
    }

    public static JSONArray getArray(double oldLng, double oldLat, double newLng, double newLat, double minLng, double maxLat, double maxLng, double minLat) {
        Point[] diagonalPoints = new Point[2];
        // one corner of the rectangle
        diagonalPoints[0] = new Point(minLng, maxLat);
        // opposite corner of the rectangle
        diagonalPoints[1] = new Point(maxLng, minLat);
        // center of the original rectangle
        Point oldCenter = new Point(oldLng, oldLat);
        // new center of the rectangle
        Point newCenter = new Point(newLng, newLat);

        System.out.println("Bearing: " + getBearing(oldCenter, newCenter));

        translateRectangle(diagonalPoints, oldCenter, newCenter);

        JSONArray array = new JSONArray();
        for (Point p : diagonalPoints) {
            array.add(p.x);
            array.add(p.y);
            System.out.println("Diagonal Point: (" + p.x + ", " + p.y + ")");
        }
        return array;
    }

}
